﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace ZG_member
{
    public partial class Worker : Form
    {
        public Worker()
        {
            InitializeComponent();
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
            this.Opacity = 0;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //timer开启
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }

        private void Worker_Load(object sender, EventArgs e)
        {
            timer1.Start();
            ZG_member.BLL.Worker sale = new ZG_member.BLL.Worker();
            this.DgvWorker.DataSource = sale.GetListWhere("").Tables[0];
            this.lbWasteDate.Visible = false;
            this.lbWasteID.Visible = false;
            this.lbWasteMemberID.Visible = false;
            this.lbWasteMoney.Visible = false;
            this.txtWasteDate.Visible = false;
            this.txtWasteID.Visible = false;
            this.txtWasteMemberID.Visible = false;
            this.txtWasteMoney.Visible = false;
            this.txtWasteDate2.Visible = false;
            this.label1.Visible = false;
            this.WasteBtn.Visible = false;
            //****************************
            this.lbListID.Visible = true;
            this.lbListMoney.Visible = true;
            this.lbListName.Visible = true;
            this.txtListID.Visible = true;
            this.txtListMoney.Visible = true;
            this.txtListName.Visible = true;
            this.ListBtn.Visible = true;
        }

        private void WasteBtn_Click(object sender, EventArgs e)
        {
            string Date = this.txtWasteDate.Text;
            string Date2 = this.txtWasteDate2.Text;
            string saleID = this.txtWasteID.Text;
            string memberID = this.txtWasteMemberID.Text;
            string Money = this.txtWasteMoney.Text;
            if (Money == "")
            {
                Money = "0";
            }
            ZG_member.BLL.WorkerWasteBook saleWaste = new ZG_member.BLL.WorkerWasteBook();
            DataTable dt = saleWaste.GetListWhere("Dates>='" + Date + "' and Dates<='"+Date2+"' and WorkerId like '%" + saleID + "%' and memberId like '%" + memberID + "%' and money>=" + Money + "").Tables[0];
            this.DgvWorkerWasteBook.DataSource = dt;
            decimal countMoney = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                countMoney += Convert.ToDecimal(dt.Rows[i]["Money"]);
            }
            DataRow row1;
            row1 = dt.NewRow();
            DataRow row2;
            row2 = dt.NewRow();
            row2["memberId"] = "总计:" + countMoney;
            dt.Rows.Add(row1);
            dt.Rows.Add(row2);
            this.DgvWorkerWasteBook.DataSource = dt;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            WorkerAdd saleAdd = new WorkerAdd(this);
            saleAdd.ShowDialog();
        }

        public void SaleUpdate()
        {
            int id = Convert.ToInt32(this.DgvWorker.CurrentRow.Cells["id"].Value);
            WorkerAdd saleAdd = new WorkerAdd(id, this);
            saleAdd.ShowDialog();
        }


        private void button3_Click(object sender, EventArgs e)
        {
            SaleUpdate();
        }

        private void DgvWorker_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            SaleUpdate();
        }

        private void DgvWorker_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    DgvWorker.ClearSelection();
                    DgvWorker.Rows[e.RowIndex].Selected = true;
                    DgvWorker.CurrentCell = DgvWorker.Rows[e.RowIndex].Cells[e.ColumnIndex];

                }
            }
        }

        private void DgvWorkerWasteBook_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
           
        }
        public void SaleDelete()
        {
            DialogResult dr = MessageBox.Show("提示", "确定删除？", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            if (dr == DialogResult.Yes)
            {
                int id = Convert.ToInt32(this.DgvWorker.CurrentRow.Cells["id"].Value);
                ZG_member.BLL.Worker sales = new ZG_member.BLL.Worker();
                sales.Delete(id);
                MessageBox.Show("删除成功");
                this.DgvWorker.DataSource = sales.GetListWhere("").Tables[0];
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            SaleDelete();
        }

        private void ListBtn_Click(object sender, EventArgs e)
        {
            string saleId = this.txtListID.Text;
            string saleName = this.txtListName.Text;
            string saleMoney = this.txtListMoney.Text;
            ZG_member.BLL.Worker saleList = new ZG_member.BLL.Worker();
            if (saleMoney == "")
            {
                saleMoney = "0";
            }
            this.DgvWorker.DataSource = saleList.GetListWhere("WorkerId like '%" + saleId + "%' and Name like '%" + saleName + "%' and Money>=" + saleMoney + "").Tables[0];
        }
        int flag = 0;
        private void Worker_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaleUpdate();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaleDelete();
        }

        private void 查看详情ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.tabControl1.SelectedIndex = 1;
            ZG_member.BLL.WorkerWasteBook salesBook = new ZG_member.BLL.WorkerWasteBook();
            string SaleId = this.DgvWorker.CurrentRow.Cells["WorkerId"].Value.ToString();
            this.DgvWorkerWasteBook.DataSource = salesBook.GetListWhere(" WorkerId = " + SaleId + "").Tables[0];
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string text = this.tabControl1.TabPages[this.tabControl1.SelectedIndex].Text;
            if (text == "施工列表")
            {
                this.lbWasteDate.Visible = false;
              
                this.lbWasteID.Visible = false;
                this.lbWasteMemberID.Visible = false;
                this.lbWasteMoney.Visible = false;
                this.txtWasteDate.Visible = false;
                this.txtWasteDate2.Visible = false;
                this.label1.Visible = false;
                this.txtWasteID.Visible = false;
                this.txtWasteMemberID.Visible = false;
                this.txtWasteMoney.Visible = false;
                this.WasteBtn.Visible = false;
                //****************************
                this.lbListID.Visible = true;
                this.lbListMoney.Visible = true;
                this.lbListName.Visible = true;
                this.txtListID.Visible = true;
                this.txtListMoney.Visible = true;
                this.txtListName.Visible = true;
                this.ListBtn.Visible = true;
                this.WasteBtn.Location = new Point(643, 17);
            }
            else
            {
                this.lbListID.Visible = false;
                this.lbListMoney.Visible = false;
                this.lbListName.Visible = false;
                this.txtListID.Visible = false;
                this.txtListMoney.Visible = false;
                this.txtListName.Visible = false;
                this.ListBtn.Visible = false;
                //******************************
                this.lbWasteDate.Visible = true;
                this.lbWasteID.Visible = true;
                this.lbWasteMemberID.Visible = true;
                this.lbWasteMoney.Visible = true;
                this.txtWasteDate.Visible = true;
                this.txtWasteID.Visible = true;
                this.txtWasteMemberID.Visible = true;
                this.txtWasteMoney.Visible = true;
                this.WasteBtn.Visible = true;
                this.txtWasteDate2.Visible = true;
                this.label1.Visible = true;
                this.WasteBtn.Location = new Point(817, 17);
            }
        }

        public void ExportExcel(string fileName, DataGridView myDGV)
        {
            string saveFileName = "";
            //bool fileSaved = false;
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.DefaultExt = "xls";
            saveDialog.Filter = "Excel文件|*.xls";
            saveDialog.FileName = fileName;
            saveDialog.ShowDialog();
            saveFileName = saveDialog.FileName;
            if (saveFileName.IndexOf(":") < 0) return; //被点了取消 
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            if (xlApp == null)
            {
                MessageBox.Show("无法创建Excel对象，可能您的机子未安装Office.Excel组件");
                return;
            }
            Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
            Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 
            int colIndex = 0;
            //写入标题
            for (int i = 0; i < myDGV.ColumnCount; i++)
            {
                if (myDGV.Columns[i].Visible)
                {
                    colIndex++;
                    worksheet.Cells[1, colIndex] = myDGV.Columns[i].HeaderText;
                    //  worksheet.Cells[1, i] = myDGV.Columns[i].HeaderText;
                }
            }
            //写入数值
            for (int r = 0; r < myDGV.Rows.Count; r++)
            {
                colIndex = 0;
                for (int i = 0; i < myDGV.ColumnCount; i++)
                {
                    if (myDGV.Columns[i].Visible)
                    {
                        colIndex++;
                        worksheet.Cells[r + 2, colIndex] = myDGV.Rows[r].Cells[i].Value;
                    }
                }
                System.Windows.Forms.Application.DoEvents();
            }
            worksheet.Columns.EntireColumn.AutoFit();//列宽自适应

            if (saveFileName != "")
            {
                try
                {
                    workbook.Saved = true;
                    workbook.SaveCopyAs(saveFileName);
                    //fileSaved = true;
                }
                catch (Exception ex)
                {
                    //fileSaved = false;
                    MessageBox.Show("导出文件时出错,文件可能正被打开！/n" + ex.Message);
                }

            }
            xlApp.Quit();
            GC.Collect();//强行销毁 
            // if (fileSaved && System.IO.File.Exists(saveFileName)) System.Diagnostics.Process.Start(saveFileName); //打开EXCEL

            MessageBox.Show(fileName + "导出成功", "系统提示", MessageBoxButtons.OK);
        }


 
        private void button4_Click(object sender, EventArgs e)
        {
            ExportExcel("", DgvWorker);
        }

        private void button5_Click(object sender, EventArgs e)
        {

            ExportExcel("", DgvWorkerWasteBook);
        }
    }
}
